package cn.edu.laud.sany.generatedata;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Random;

import cn.edu.laud.sany.utils.MyLog;

public class SanyCSVWriter extends Thread
{
	protected MyLog logger;
	protected static Integer fileCount = 0;
	protected static int dataCount = 0;
	public static HashMap<String, Long> equipMapGen = SanyGenerateData.equipMapGen;
	public static Integer[] paraArray;
	private StringBuilder sb = new StringBuilder();

	public SanyCSVWriter(int no)
	{
		logger = MyLog.getMyLog(SanyGenerateData.getLogFilePath(), true);
		logger.setThreadName("Thread " + no);
	}

	private void makeRowLine(int genNum, int paraId, String equipCode,
			String paravalue, String localTime)
	{
		sb.append("\"");
		sb.append(genNum);
		sb.append("\",\"");
		sb.append(equipCode);
		sb.append("\",\"");
		sb.append(paraId);
		sb.append("\",\"");
		sb.append(paravalue);
		sb.append("\",\"\",\"");
		sb.append(localTime);
		sb.append("\"\r\n");
	}

	protected long writeFile(String fileName)
	{
		Random ran = new Random();
		BufferedWriter writer = null;
		long filesize = SanyGenerateData.oneFileSize;
		try
		{
			try
			{
				writer = new BufferedWriter(new FileWriter(fileName));
			}
			catch (FileNotFoundException e3)
			{
				e3.printStackTrace();
				logger.error(e3.getMessage());
				return 0;
			}

			try
			{
				boolean finish = false;
				while (finish == false)
				{
					Iterator<Entry<String, Long>> iter = equipMapGen.entrySet()
							.iterator();
					while (iter.hasNext())
					{
						Entry<String, Long> item = iter.next();
						String equipCode = item.getKey();
						long equipsize = item.getValue();
						for (int i = 0; i < equipsize; i++)
						{
							int paraId = paraArray[ran
									.nextInt(paraArray.length)];
							SanyParaInfo paraInfo = SanyGenerateData.paraMap
									.get(paraId);
							String paravalue = paraInfo.getRandomValues();
							String localTime = paraInfo.getRandomDate();
							makeRowLine(dataCount, paraId, equipCode,
									paravalue, localTime);
							dataCount++;
							if (dataCount % 100 == 0)
							{
								String res = sb.toString();
								writer.write(res);
								sb = new StringBuilder();
							}
							if (dataCount % 100000 == 0)
							{
								logger.info("generate count = " + dataCount);
							}
							if (dataCount > filesize)
							{
								finish = true;
								break;
							}
						}
					}
				}
			}
			catch (IOException e1)
			{
				e1.printStackTrace();
				logger.error(e1.getMessage());
			}
			finally
			{
				try
				{
					if (sb.length() > 0)
					{
						String res = sb.toString();
						writer.write(res);
					}
					writer.flush();
					writer.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
				catch (NullPointerException e1)
				{
					e1.printStackTrace();
					logger.error(e1.getMessage());
				}
			}
		}
		catch (Throwable t)
		{
			t.printStackTrace();
			logger.error(t.getMessage());
		}

		return filesize;
	}

	@Override
	public void run()
	{
		paraArray = SanyGenerateData.paraArray;
		equipMapGen = SanyGenerateData.equipMapGen;
		int fileNum = 0;
		synchronized (fileCount)
		{
			fileNum = fileCount;
			fileCount++;
		}
		String fileName = "data" + fileNum + ".csv";
		logger.info("generate file: " + fileName);
		long total = writeFile(SanyGenerateData.csvOutputFolder + "/"
				+ fileName);
		logger.info("generate file finished! count = " + total);
	}

}
